package engine.system;

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


public abstract class SimpleWindow extends Window implements ActionListener 
{
	private JFrame app;
	protected JButton bpp;
	private JCheckBox box;
	private ComboBoxModel cbox;
	private ComboBoxModel c1;
	private JComboBox jcom;
	private JComboBox j1;
	private JLabel text;
	private String s[] = new String[] { "320 x 200","320 x 240", "400 x 300"
			, "480 x 360","512 x 384", "640 x 400","640 x 480", "800 x 600",
			"1024 x 768", "1280 x 800" };
	private int i[]= new int[2];
	private Icon joge= new ImageIcon(SimpleWindow.class.getClassLoader().getResource("system/JOGE_300x200.png"));
	public SimpleWindow()
	{
		initLookAndFeel();
		app = new JFrame("Joge Einstellungen");
		app.setSize(420, 300);
		app.setResizable(false);
		app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		int x = (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth() 
				- app.getWidth()) / 2;
		int y = (int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight() 
				- app.getHeight()) / 2;
		app.setLocation(x, y);
		app.setLayout(null);

		JPanel panel = new JPanel();
		panel.setLayout(null);
		panel.setBounds(0, 0, 420, 300);
		panel.setLayout(null);
		app.add(panel);

		bpp = new JButton("OK");
		bpp.setBounds(320, 230, 60, 20);
		bpp.setActionCommand("OK");
		bpp.addActionListener(this);
		panel.add(bpp);

		box = new JCheckBox("fullsecreen?", false);
		box.setBounds(220, 230, 100, 20);
		panel.add(box);

		cbox = new DefaultComboBoxModel(s);
		jcom = new JComboBox();
		jcom.setModel(cbox);
		jcom.setBounds(100, 230, 100, 20);
		panel.add(jcom);
		
		c1 = new DefaultComboBoxModel(new String[] { "16bpp", "32bpp" });
		j1 = new JComboBox();
		j1.setModel(c1);
		j1.setBounds(20, 230, 60, 20);
		panel.add(j1);
		
		text = new JLabel();
		text.setBounds(55, 10, 400, 200);
		text.setIcon(joge);
		panel.add(text);
		
		app.setVisible(true);
	}
	private void initLookAndFeel()
	{
		try
		{
			javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager
					.getSystemLookAndFeelClassName());
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	public void action()
	{
		getResolution();
		setResolution(getX(), getY());
		setFullscreen(getFullscreen());
		setBpp(getBpp());
		app.dispose ();
		initEngine();
		System.exit(0);
	}
	private boolean getFullscreen()
	{
		return box.isSelected();
	}
	private String getBpp()
	{
		String g=""+j1.getSelectedItem();
		return g.substring(0, 1);
	}
	private void getResolution()
	{
		String r,g,w;
		g=(String)jcom.getSelectedItem();
		if(g.length()<10)
		{
			r=g.substring(0, 3);
			w=g.substring(6, 9);
		}
		else 
		{
			r=g.substring(0, 4);
			w=g.substring(7, 10);
		}
		i[0]=Integer.parseInt(r);
		i[1]=Integer.parseInt(w);
	}
	private int getX()
	{
		return i[0];
	}
	private int getY()
	{
		return i[1];
	}
	public void actionPerformed(ActionEvent e)
	{
		if(e.getActionCommand().equals("OK")) 
		{
		    action();
		 }
	}
}
